<?php

class ProductController extends Controller
{
	public function actionIndex()
	{
	  $categoryID = isset($_GET['catID']) ? $_GET['catID'] : '';
	  $products = Product::model()->getProducts($categoryID);
		$this->render('index', array(
		  'products' => $products
		));
	}
  
   public function actionDetail($id)
	{
	  $model = $this->loadModel($id);
	  $criteria = Product::model()->search()->criteria;
	  $criteria->addCondition("t.id = $model->id");
	  $product = Product::model()->find($criteria);
    $category = Category::model()->getName($model->categoryID);
    
    $related_products = Product::model()->getProducts(md5($model->categoryID), $model->id);
    
		$this->render('detail', array(
		  'product' => $product,
		  'category' => $category,
		  'related_products' => $related_products
		));
	}
	
	
	public function loadModel($id)
	{
	  $model = Product::model()->find(array(
	    "condition" => "md5(`id`) = '$id' AND enabled = 1"
	  ));
	  
	  if ($model === null)
	    throw new CHttpException(404,'The requested page does not exist.');
    return $model;
	}
}